<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 分类ajax类
 * @package  	ajax
 * @subpackage  backend
 * @create by allen.qiang
 */

quanli_require('module/base/goodsHandler.class.php');

class ajax_goodsCate {

    public function execute() {
        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {
            switch($_REQUEST['operFlg']) {

                case 1: //张开商品分类列表
                    $this->addCate();
                    break;
                case 2: //收起商品分类列表
                    $this->removeCate();
                    break;
                case 3://分类是否禁用
                    $this->isusecategory();
                    break;
                case 4: //删除商品分类
                    $this->deletecategory();
                    break;

            }
        }
    }

    /**
     * 点击显示分类
     */
    private function addCate() {

        global $G_PGLOB;

        $cate_id = $_REQUEST['cate_id'];
        $mid = isset($_REQUEST['mid'])?$_REQUEST['mid']:'';

        if($mid != '' && $mid == 'mid'){
            $cate_row = $G_PGLOB->DBCA->stmt_query('select * from goods_smallcategory where midCategoryID =? ','i',$cate_id);//三级分类
        }else{
            $cate_row = $G_PGLOB->DBCA->stmt_query('select * from goods_midcategory where bigCategoryID =? ','i',$cate_id);//二级分类
        }

        $str ='';
        $img = '';
        $cate_id= '';
        $cid = '';
        $trid = '';
        $class = '';
        $margin_left = '';
        $float = '';
        foreach($cate_row as $val){

            if($mid != '' && $mid == 'mid'){
                $cate_id = $val->smallCategoryID; //三级分类ID
                $cid = 'sid'; //分类标识
                $trid = 'scate'; //table 行ID前缀
                $cate_type = '小分类';
                $class = 'style="text-align:left;"';
                $cat_str = $val->bigCategoryID.'_'.$val->midCategoryID.'_'.$val->smallCategoryID;
                $margin_left = 'style = "margin-left:50px"';
            }else{

                $cate_id = $val->midCategoryID;//二级分类ID
                $cid = 'mid'; //分类标识
                $trid = 'mcate'; //table 行ID前缀
                $cate_type = '中分类';
                $class = 'style="text-align:left;"';
                $cat_str = $val->bigCategoryID.'_'.$val->midCategoryID;
                $float = 'style = "float:left;margin-left:25px"';
                //判断二级分类下是否存在三级分类
                $catethree = $G_PGLOB->DBCA->stmt_query('select count(*) as count from goods_smallcategory where midCategoryID = ? ','i',$cate_id);
                if($catethree[0]->count > 0){
                    $img = '<img id="img_add_1" onclick="category.add_cate('.$cate_id.',\''.$cid.'\')" src="/quanliadmin/docroot/images/bag_open.gif">&nbsp;';
                }else{
                    $img =  '';
                    $margin_left = 'style = "margin-left:25px"';
                }
            }
			
            if($G_PGLOB->has_permission(502)){
	            if($val->useFlg == 1)
	                $use = '<div style="text-align:center" id = "useflg_'.$cat_str.'"><a onclick = "category.doAction(\''.$cat_str.'\',0)">启用</a></div>';
	            else
	                $use = '<div style="text-align:center" id = "useflg_'.$cat_str.'"><a onclick = "category.doAction(\''.$cat_str.'\',1)">禁用</a></div>';
            }else{
            	if($val->useFlg == 1)
	                $use = '<div style="text-align:center;">启用</div>';
	            else
	                $use = '<div style="text-align:center;">禁用</div>';
            }

            $str  .= '<tr id="'.$trid.$cate_id.'">';
            $str  .= '<td '.$class.'><div '.$float.'  id="tdone_'.$cate_id.'">'.$img.'</div><div '.$margin_left.'>'.$cate_id.'<span style="font-size:13px;font-weight:bold;">【'.$val->description.'】</span>'.'</div></td>';
            $str  .= '<td class = "tal">'.$cate_type.'</td>';
            $str  .= '<td>'.$use.'</td>';
            if($G_PGLOB->has_permission(502)){
	            $str  .= '<td style="text-align:center">
	            <a onclick = "category.cateedit(\''.$cat_str.'\')">编辑</a>
	            </td>';
            }
            $str  .= '</tr>';
        }
        $str_arr = array('tr'=>$str);

        echo json_encode( $str_arr );
    }

    /**
     *点击移除分类
     */
    private function removeCate(){
        global $G_PGLOB;
        $cate_id = $_REQUEST['cate_id'];
        $mid = isset($_REQUEST['mid'])?$_REQUEST['mid']:'';
        $midcate_arr = array();
        //判断是否剔除中分类 小分类
        if($mid == ''){
            $cate_row = $G_PGLOB->DBCA->stmt_query('select midCategoryID from goods_midcategory where bigCategoryID =? ','i',$cate_id);
            foreach($cate_row as $val){
                $midcate_arr[] = $val->midCategoryID;
                $catesmall_row = $G_PGLOB->DBCA->stmt_query('select smallCategoryID from goods_smallcategory where midCategoryID =? ','i',$val->midCategoryID);
                if($catesmall_row){
                    foreach($catesmall_row as $small_val){
                        $midcate_arr[] = $small_val->smallCategoryID;
                    }
                }
            }
        }else{
            $catesmall_row = $G_PGLOB->DBCA->stmt_query('select smallCategoryID from goods_smallcategory where midCategoryID =? ','i',$cate_id);
            foreach($catesmall_row as $small_val){
                $midcate_arr[] = $small_val->smallCategoryID;
            }
        }
        echo json_encode( $midcate_arr );
    }

    /*
     * 禁用使用分类
     */
    private function isusecategory () {
        global $G_PGLOB;
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
        $result_array = $goodsHandler->isusecategory( $_REQUEST ) ;
        echo json_encode($result_array);
    }

    /*
     * 删除分类
     */
    private function deletecategory () {
        global $G_PGLOB;
        $goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
        $result_array = $goodsHandler->deletecate( $_REQUEST ) ;
        echo json_encode($result_array);
    }

}
